package com.ydc.ln.tutorial.algorithm;

import org.junit.Test;

public class Sort01Test {

	@Test
	public void testT() {
		System.out.println("A===>" + (int) 'A');
		System.out.println("Z===>" + (int) 'Z');
		System.out.println("a===>" + (int) 'a');
		System.out.println("z===>" + (int) 'z');
	}

	/**
	 * 一系列英文单词排序
	 * 
	 * "Bc","Ad","aC","Hello","X man","little","During","day"
	 * 
	 * <pre></pre>
	 * 
	 * "Ad","aC", "Bc","During","day","Hello","little", "X man"
	 * 
	 * <pre>
	 * 排序规则:
	 * 1.字母ABC, 
	 * 2.区分大小写A&gt;a
	 * </pre>
	 */
	@Test
	public void testSort01() {
		String[] strArray = { "Bc", "Ad", "aC", "Hello", "X man", "little", "During", "day" };
		System.out.println("排序前：");
		for (String str : strArray) {
			System.out.print(str + ", ");
		}
		System.out.println();
		
		String temp;
		for (int i = 1; i < strArray.length; i++) {
			for (int j = i; j < strArray.length; j++) {
				if (sort(strArray[i - 1], strArray[j]) > 0) {
					temp = strArray[i - 1];
					strArray[i - 1] = strArray[j];
					strArray[j] = temp;
				}
			}
		}
		
		System.out.println("排序后：");
		for (String str : strArray) {
			System.out.print(str + ", ");
		}
	}

	@Test
	public void testSortSimple() {
		int[] iArray = { 2, 1, 7, 5, 6, 3 };
		for (int i : iArray) {
			System.out.print(i + " ");
		}
		System.out.println();
		int temp;
		for (int i = 1; i < iArray.length; i++) {
			for (int j = i; j < iArray.length; j++) {
				if (iArray[i - 1] > iArray[j]) {
					temp = iArray[i - 1];
					iArray[i - 1] = iArray[j];
					iArray[j] = temp;
				}
			}
		}

		for (int i : iArray) {
			System.out.print(i + " ");
		}
		System.out.println();

	}

	/**
	 * 两个字符串比较
	 * 
	 * @param str1
	 * @param srt2
	 * @return 0:str1=str2 大于0:str1>str2 小于0:str1<str2
	 */
	public int sort(String str1, String str2) {
		int len = Math.min(str1.length(), str2.length());
		byte[] b1Array = str1.getBytes();
		byte[] b2Array = str2.getBytes();
		for (int i = 0; i < len; i++) {
			if (b1Array[i] != b2Array[i]) {
				if (b1Array[i]>=65 && b1Array[i] <= 90
						&& b1Array[i]>=97 && b1Array[i] <= 122
						&& b2Array[i]>=65 && b2Array[i] <= 90
						&& b2Array[i]>=97 && b2Array[i] <= 122 
						&& Math.abs(b1Array[i] - b2Array[i]) == 32) {
					return (b2Array[i] - b1Array[i]);
				}
				return (str1.toLowerCase().charAt(i) - str2.toLowerCase().charAt(i));
			}
		}
		return len == Math.max(str1.length(), str2.length()) ? 0 : 1;
	}
}
